/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package meminin.naivebayes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import meminin.model.DataRecord;
import meminin.model.MySqlDataRecord;

/**
 *
 * @author Danang
 */
public class NaiveBayesDataRecord extends MySqlDataRecord {
    public String toString() {
        return data.toString();
    }

    @Override
    public boolean equals(Object obj) {
        return this.data.equals(((NaiveBayesDataRecord) obj).getData());
    }

    public void setData(HashMap<String, String> data) {
        this.data = data;
    }

    public HashMap<String, String> getData() {
        return data;
    }
    private HashMap<String, String> data;

    public NaiveBayesDataRecord() {
        data = new HashMap<String, String>();
    }

    public void setAttributeValue(String key, String value) {
        data.put(key, value);
    }

    public String getAttributeValue(String key) {
        return data.get(key);
    }

    public ArrayList<String> getKeys() {
        ArrayList<String> tempKeys = new ArrayList<String>();
        Iterator it = data.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            tempKeys.add((String) pairs.getKey());
        }
        return tempKeys;
    }

    public DataRecord getCopy() {
        NaiveBayesDataRecord tempRecord = new NaiveBayesDataRecord();
        tempRecord.setData((HashMap<String, String>) this.data.clone());
        return tempRecord;
    }

    @Override
    public String getInsertQuery(String dbName, String tableName) {
        ArrayList<String> attributes = this.getKeys();
        String attributesString = attributes.get(0);
        String valuesString = "'" + getAttributeValue(attributes.get(0)) + "'";
        attributes.remove(0);
        for (String attr : attributes) {
            attributesString += ", " + attr;
            valuesString += ", '" + getAttributeValue(attr) + "'";
        }
        return "INSERT INTO " + dbName + "." + tableName + " (" + attributesString + ") VALUES (" + valuesString + ")";
    }

    @Override
    public String getReadQuery(String dbName, String tableName) {
        String query = "SELECT * FROM " + dbName + "." + tableName + " WHERE 1 = 1 ";
        ArrayList<String> attributes = this.getKeys();
        for (String attr : attributes) {
            query += " AND " + attr + " = '" + getAttributeValue(attr) + "'";
        }
        return query;
    }
    
    public String getReadQueryWhereClass(String dbName, String tableName, String classDesc, String classValue) {
        String query = "SELECT * FROM " + dbName + "." + tableName + " WHERE " + classDesc +" = '" + classValue + "'";
        return query;
    }
    
    public String getReadQueryWhereClassAndAttrib(String dbName, String tableName, 
            String classDesc, String classValue, String attribDesc, String attribValue) {
        String query = "SELECT * FROM " + dbName + "." + tableName + " WHERE "+attribDesc+" = '"+attribValue+"' AND " + classDesc +" = '" + classValue + "'";
        return query;
    }

    @Override
    public String getUpdateQuery(String dbName, String tableName, DataRecord newData) {
        String updateStat = "UPDATE " + dbName + "." + tableName;
        // fill in the set statement 
        NaiveBayesDataRecord copied = (NaiveBayesDataRecord) newData;
        ArrayList<String> attributes = (ArrayList<String>) copied.getKeys().clone();
        if (attributes.isEmpty()) {
            return "";
        }
        String setStat = " SET " + attributes.get(0) + " = '" + copied.getAttributeValue(attributes.get(0)) + "'";
        attributes.remove(0);
        for (String attr : attributes) {
            setStat += ", " + attr + " = '" + copied.getAttributeValue(attr) + "'";
        }
        // fill in the condition
        String condition = " WHERE 1 = 1 ";
        attributes = (ArrayList<String>) this.getKeys().clone();
        for (String attr : attributes) {
            condition += " AND " + attr + " = '" + getAttributeValue(attr) + "'";
        }
        // return the complete query
        return updateStat + setStat + condition;
    }

    @Override
    public String getDeleteQuery(String dbName, String tableName) {
        String query = "DELETE FROM " + dbName + "." + tableName + " WHERE 1 = 1 ";
        ArrayList<String> attributes = this.getKeys();
        for (String attr : attributes) {
            query += " AND " + attr + " = '" + getAttributeValue(attr) + "'";
        }
        return query;
    }
}
